#pragma once

#include <iostream>
#include <cstdio>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include "Log.hpp"


using namespace std;

#define PATH_NAME "/home/Lxy/code"
#define PROJ_ID 0x14
#define MEM_SIZE 4096

#define FIFO_FILE ".fifo"

#define READER O_RDONLY
#define WRITER O_WRONLY



void CreateFifo()
{
    umask(0);
    if(mkfifo(FIFO_FILE,0666) < 0)
    {
        Log()<<strerror(errno)<< endl;
        exit(2);
    }
}

key_t CreatKey()
{
    key_t key = ftok(PATH_NAME,PROJ_ID);
    if(key < 0 )
    {
        cerr<< "ftok:" << strerror(errno) <<endl;
        exit(1);
    }

    return key;
}

int Open(const std::string& filename,int flags)
{
    return open(filename.c_str(),flags);
}

int Wait(int fd)
{
    uint32_t values = 0;
    ssize_t s = read(fd,&values,sizeof(values));
    assert(s == sizeof(values));
    (void)s;

    return s;
}

void Signal(int fd)
{
    uint32_t cmd = 1;
    write(fd,&cmd,sizeof(cmd));
}

void Close(int fd,const std::string filename)
{
    close(fd);
    unlink(filename.c_str());
}
